import sys 
sys.path.append('code')
from feature_eng.alpha_series.alpha_utility import *


class Alpha_ser():
    bound_ratio = pd.read_pickle(r'data\stock_data\daily\other\bound_ratio.pkl.gzip')
    bound_ratio = bound_ratio.fillna(method='ffill')
    currency_boc_safe = pd.read_pickle(r'data\stock_data\daily\other\currency_boc_safe.pkl.gzip')
    currency_boc_safe = currency_boc_safe.fillna(method='ffill')
    rate_interbank = pd.read_pickle(r'data\stock_data\daily\other\rate_interbank.pkl.gzip')
    pepb_sz = pd.read_pickle(r'data\stock_data\daily\other\pepb_sz.pkl.gzip')
    pepb_sh = pd.read_pickle(r'data\stock_data\daily\other\pepb_sh.pkl.gzip')
    pepb_kc = pd.read_pickle(r'data\stock_data\daily\other\pepb_kc.pkl.gzip')
    pepb_cy = pd.read_pickle(r'data\stock_data\daily\other\pepb_cy.pkl.gzip')
    pepb_50 = pd.read_pickle(r'data\stock_data\daily\other\pepb_000016.pkl.gzip')
    pepb_300 = pd.read_pickle(r'data\stock_data\daily\other\pepb_000300.pkl.gzip')
    pepb_1000 = pd.read_pickle(r'data\stock_data\daily\other\pepb_000852.pkl.gzip')
    pepb_500 = pd.read_pickle(r'data\stock_data\daily\other\pepb_000905.pkl.gzip')
    ebs_lg = pd.read_pickle(r'data\stock_data\daily\other\ebs_lg.pkl.gzip')
    ebs_lg = ebs_lg.fillna(method='ffill')

    def __init__(self, stock_df):
        stock_df = stock_df.fillna(method='ffill')
        self.pe = stock_df['pe'] 
        self.pettm = stock_df['pe_ttm'] 
        self.pb = stock_df['pb']  
        self.ps = stock_df['ps']
        self.psttm = stock_df['ps_ttm']
        self.dv_ratio = stock_df['dv_ratio']
        self.dv_ttm = stock_df['dv_ttm']
        self.volume = stock_df['volume']
        self.total_mv = stock_df['total_mv']#总市值
        self.volume = stock_df['volume']
        self.close = stock_df['close']
        self.lagret = stock_df['close']/stock_df['close'].shift(1)
        self.ampli = (stock_df['high']-stock_df['low'])/stock_df['close'].shift(1) 
        self.get_other_data()

    def get_other_data(self):
        self.bound_ratio = Alpha_ser.bound_ratio
        self.currency_boc_safe = Alpha_ser.currency_boc_safe
        self.rate_interbank = Alpha_ser.rate_interbank
        self.pepb_sz = Alpha_ser.pepb_sz
        self.pepb_sh = Alpha_ser.pepb_sh
        self.pepb_kc = Alpha_ser.pepb_kc
        self.pepb_cy = Alpha_ser.pepb_cy
        self.pepb_50 = Alpha_ser.pepb_50
        self.pepb_300 = Alpha_ser.pepb_300
        self.pepb_1000 = Alpha_ser.pepb_1000
        self.pepb_500 = Alpha_ser.pepb_500
        self.ebs_lg = Alpha_ser.ebs_lg 

class Alpha_ser0(Alpha_ser):
    def __init__(self, stock_df):
        super().__init__(stock_df)

    def alpha_ser0_0(self):
        return self.total_mv

    def alpha_ser0_1(self):
        return self.pettm - self.pe

    def alpha_ser0_2(self):
        return self.psttm - self.ps

    def alpha_ser0_3(self):
        return self.dv_ttm - self.dv_ratio 

    def alpha_ser0_4(self):
        return self.bound_ratio['中国国债收益率2年']-self.dv_ttm

    def alpha_ser0_5(self):
        return self.bound_ratio['美国国债收益率10年']-self.dv_ttm

    def alpha_ser0_6(self):
        return self.bound_ratio['中国国债收益率10年']-self.dv_ttm

    def alpha_ser0_7(self):
        return self.bound_ratio['中国国债收益率30年']-self.dv_ttm

    def alpha_ser0_8(self):
        return self.bound_ratio['中国国债收益率10年-2年']-self.dv_ttm

    def alpha_ser0_9(self):
        return self.ebs_lg['股债利差'] / ser_expand_rank(self.close)

    def alpha_ser0_10(self):
        return self.rate_interbank['涨跌'] - self.lagret

    # def alpha_ser0_11(self):
    #     return self.ebs_lg['股债利差'] 

    # def alpha_ser0_12(self):
    #     return self.rate_interbank['涨跌'] 

    def alpha_ser0_13(self):
        return self.rate_interbank['利率'] / ser_expand_rank(self.close)

    # def alpha_ser0_14(self):
    #     ebs = self.ebs_lg['股债利差']
    #     return (ebs - min_roll(ebs, 1000))/(max_roll(ebs,1000) -min_roll(ebs, 1000))

    def alpha_ser0_15(self):
        ebs = self.ebs_lg['股债利差']/ ser_expand_rank(self.close)
        return (ebs - min_roll(ebs, 1000))/(max_roll(ebs,1000) -min_roll(ebs, 1000))


class Alpha_ser1(Alpha_ser):
    def __init__(self, stock_df):
        super().__init__(stock_df)

    def alpha_ser1_0(self, N):
        return reg_beta1(ma_roll(self.ebs_lg['股债利差'], N), ma_roll(self.close, N), N)

    def alpha_ser1_1(self, n):
        x = sum_roll(self.pepb_sh['addTtmPe']-self.pettm, n)
        y = self.lagret
        return reg_beta1(x, y, n)

    def alpha_ser1_2(self, N):
        return ser_corr(self.volume, self.lagret, N)

    def alpha_ser1_3(self, n):
        return ser_rank(self.pepb_300['close'], n) - ser_rank(self.close, n)
    
    def alpha_ser1_4(self, n):
        return ser_rank(self.pepb_1000['close'], n) - ser_rank(self.close, n)

    def alpha_ser1_5(self, n):
        return reg_beta1(self.ebs_lg['股债利差'], self.dv_ttm, n)

    def alpha_ser1_6(self, n):
        return reg_beta0(self.ebs_lg['股债利差']-self.dv_ttm, self.lagret, n)
       
    def alpha_ser1_7(self, n):
        return reg_beta0(self.pepb_sh['addTtmPe']-self.pettm, sum_roll(self.lagret, n), n) 

    def alpha_ser1_8(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_sh['close']/self.pepb_sh['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta1(sh ,stock, n)

    def alpha_ser1_9(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_sz['close']/self.pepb_sz['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta1(sh ,stock, n)

    def alpha_ser1_10(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_kc['close']/self.pepb_kc['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta1(sh ,stock, n)

    def alpha_ser1_11(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_cy['close']/self.pepb_cy['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta1(sh ,stock, n)

    def alpha_ser1_12(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_50['close']/self.pepb_50['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta1(sh ,stock, n)
    
    def alpha_ser1_13(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_500['close']/self.pepb_500['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta1(sh ,stock, n)
    
    def alpha_ser1_14(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_300['close']/self.pepb_300['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta1(sh ,stock, n)

    def alpha_ser1_15(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_1000['close']/self.pepb_1000['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta1(sh ,stock, n)

    def alpha_ser1_16(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_sh['close']/self.pepb_sh['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta0(sh ,stock, n)

    def alpha_ser1_17(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_sz['close']/self.pepb_sz['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta0(sh ,stock, n)

    def alpha_ser1_18(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_kc['close']/self.pepb_kc['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta0(sh ,stock, n)

    def alpha_ser1_19(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_cy['close']/self.pepb_cy['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta0(sh ,stock, n)

    def alpha_ser1_20(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_50['close']/self.pepb_50['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta0(sh ,stock, n)
    
    def alpha_ser1_21(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_500['close']/self.pepb_500['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta0(sh ,stock, n)
    
    def alpha_ser1_22(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_300['close']/self.pepb_300['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta0(sh ,stock, n)

    def alpha_ser1_23(self, n):
        stock = self.lagret.rolling(n, min_periods=1).sum()
        sh_lagret = self.pepb_1000['close']/self.pepb_1000['close'].shift(1)
        sh = sh_lagret.rolling(n, min_periods=1).sum()
        return reg_beta0(sh ,stock, n)

    def alpha_ser1_24(self, n):
        sh_lagret = self.pepb_sz['close']
        return ser_rank(sh_lagret, n) - ser_rank(self.close, n)

    def alpha_ser1_25(self, n):
        sh_lagret = self.pepb_sh['close']
        return ser_rank(sh_lagret, n) - ser_rank(self.close, n)

    def alpha_ser1_26(self, n):
        sh_lagret = self.pepb_kc['close']
        return ser_rank(sh_lagret, n) - ser_rank(self.close, n)

    def alpha_ser1_27(self, n):
        sh_lagret = self.pepb_cy['close']
        return ser_rank(sh_lagret, n) - ser_rank(self.close, n)

    def alpha_ser1_28(self, n):
        sh_lagret = self.pepb_50['close']
        return ser_rank(sh_lagret, n) - ser_rank(self.close, n)

    def alpha_ser1_29(self, n):
        sh_lagret = self.pepb_500['close']
        return ser_rank(sh_lagret, n) - ser_rank(self.close, n)

    def alpha_ser1_30(self, n):
        return reg_beta0(self.ebs_lg['股债利差']-self.dv_ttm, self.lagret, n)

    def alpha_ser1_31(self, N):
        return ser_corr(sum_roll(self.rate_interbank['涨跌'],N), sum_roll(self.lagret, N), N) 

    def alpha_ser1_32(self, N):
        return N - argmax_roll(self.close, N)
    
    def alpha_ser1_32(self, N):
        return N - argmin_roll(self.close, N)

    def alpha_ser1_33(self, N):
        return N - argmax_roll(self.lagret, N)
    
    def alpha_ser1_34(self, N):
        return N - argmin_roll(self.lagret, N)

    def alpha_ser1_35(self, N):
        wavg = sum_roll(self.lagret * self.volume, N) / sum_roll(self.volume, N)
        return N - argmax_roll(wavg, N)

    def alpha_ser1_36(self, N):
        wavg = sum_roll(self.lagret * self.volume, N) / sum_roll(self.volume, N)
        return N - argmin_roll(wavg, N)

    def alpha_ser1_37(self, N):
        ebs = self.ebs_lg['股债利差']
        ebs_loc = (ebs - min_roll(ebs, N))/(max_roll(ebs,N) -min_roll(ebs, N))
        close_loc = (self.close - min_roll(self.close, N))/(max_roll(self.close,N) -min_roll(self.close, N))
        return ebs_loc - close_loc

    def alpha_ser1_38(self, N):
        ebs = self.ebs_lg['股债利差']
        return ((ebs - min_roll(ebs, N))/(max_roll(ebs,N) -min_roll(ebs, N))) - ser_expand_rank(self.close)



class Alpha_ser_stastic(Alpha_ser):
    def __init__(self, stock_df):
        super().__init__(stock_df)

    def return_stastic(self, target, N, bid='pepb_sz'):
        close = self.lagret - np.log(eval(f'self.{bid}')['close']/ eval(f'self.{bid}')['close'].shift(1)) 
        lagRet_vol = close
        if  target == "max":
            f = lagRet_vol.rolling(N, min_periods=1).max()
        if  target == "min":
            f = lagRet_vol.rolling(N, min_periods=1).min()
        if  target == "mean":
            f = lagRet_vol.rolling(N, min_periods=1).mean()
        if  target == "median":
            f = lagRet_vol.rolling(N, min_periods=1).median()
        if  target == "skew":
            f = lagRet_vol.rolling(N, min_periods=1).skew()
        if  target == "kurt":
            f = lagRet_vol.rolling(N, min_periods=1).kurt()
        return f
        

class Alpha_ser2(Alpha_ser):
    def __init__(self, stock_df):
        super().__init__(stock_df)

    def alpha_ser2_0(self,s,l):
        sh_lagret = self.pepb_sz['close']
        return ma_short_to_long(sh_lagret, s,l) - ma_short_to_long(self.close, s,l)
  
    def alpha_ser2_1(self,s,l):
        sh_lagret = self.pepb_sh['close']
        return ma_short_to_long(sh_lagret, s,l)- ma_short_to_long(self.close, s,l)

    def alpha_ser2_2(self,s,l):
        sh_lagret = self.pepb_kc['close']
        return ma_short_to_long(sh_lagret, s,l)- ma_short_to_long(self.close, s,l)
      
    def alpha_ser2_3(self,s,l):
        sh_lagret = self.pepb_cy['close']
        return ma_short_to_long(sh_lagret, s,l)- ma_short_to_long(self.close, s,l)
        
    def alpha_ser2_4(self,s,l):
        sh_lagret = self.pepb_50['close']
        return ma_short_to_long(sh_lagret, s,l) - ma_short_to_long(self.close, s,l)
        
    def alpha_ser2_5(self,s,l):
        sh_lagret = self.pepb_500['close']
        return ma_short_to_long(sh_lagret, s,l) - ma_short_to_long(self.close, s,l)
        
    def alpha_ser2_6(self,s,l):
        sh_lagret = self.pepb_300['close']
        return ma_short_to_long(sh_lagret, s,l) - ma_short_to_long(self.close, s,l)

    def alpha_ser2_7(self,s,l):
        sh_lagret = self.pepb_1000['close']
        return ma_short_to_long(sh_lagret, s,l) - ma_short_to_long(self.close, s,l)

    def alpha_ser2_8(self,s,l):
        return ma_short_to_long(self.ebs_lg['股债利差'], s,l) - ma_short_to_long(self.close, s,l)

    def alpha_ser2_9(self, s, l): 
        return ma_short_to_long(self.ampli, s, l)

    def alpha_ser2_10(self, s, l): 
        return ma_short_to_long(self.pettm, s, l)

    def alpha_ser2_11(self, s, l):
        return ma_short_to_long(self.pb, s, l)

    def alpha_ser2_12(self, s, l):
        return ma_short_to_long(self.psttm, s, l)

    def alpha_ser2_13(self, s, l):
        return ma_short_to_long(self.dv_ttm, s, l)

    def alpha_ser2_14(self, s, l):
        return ma_short_to_long(self.total_mv, s, l)

    def alpha_ser2_15(self,s,l):
        return ma_short_to_long(self.ebs_lg['股债利差']/ser_expand_rank(self.close), s,l)

    def alpha_ser2_16(self,s,l):
        return ma_short_to_long(self.rate_interbank['利率'] / ser_expand_rank(self.close), s,l)

    def alpha_ser2_17(self,s,l):
        return ma_short_to_long(self.rate_interbank['利率'], s,l) - ma_short_to_long(self.close, s,l)



class Alpha_ser3(Alpha_ser):
    def __init__(self, stock_df):
        super().__init__(stock_df)

    def alpha_ser3_0(self, c, p, n):
        return ma_cur_to_pre(self.psttm, c, p, n)

    def alpha_ser3_1(self, c, p, n):
        return ma_cur_to_pre(self.dv_ttm, c, p, n)

    def alpha_ser3_2(self, c, p, n):
        return ma_cur_to_pre(self.total_mv, c, p, n)

    def alpha_ser3_3(self, c, p, n): 
        return ma_cur_to_pre(self.pettm-self.pe, c, p, n)

    def alpha_ser3_4(self, c, p, n):
        return ma_cur_to_pre(self.psttm-self.ps, c, p, n)

    def alpha_ser3_5(self, c, p, n):
        return ma_cur_to_pre(self.dv_ttm-self.dv_ratio, c, p, n)

    def alpha_ser3_6(self, c, p, n):
        return ma_cur_to_pre(self.ampli, c, p, n) 

    def alpha_ser3_7(self, c, p, n):
        return ma_cur_to_pre(self.pepb_sh['addTtmPe']-self.pettm, c, p, n)

    def alpha_ser3_8(self, c, p, n):
        return ma_cur_to_pre(self.pepb_sh['addPb']-self.pb, c, p, n)

    def alpha_ser3_9(self, c, p, n):
        return ma_cur_to_pre(self.pepb_sh['averageTtmPe']-self.pettm, c, p, n)

    def alpha_ser3_10(self, c, p, n):
        return ma_cur_to_pre(self.pepb_sh['averagePb']-self.pb, c, p, n)

    def alpha_ser3_11(self,c, p, n):
        return ma_cur_to_pre(self.ebs_lg['股债利差']/self.dv_ttm, c, p, n) 
    
    def alpha_ser3_12(self,c, p, n):
        return ma_cur_to_pre(self.ebs_lg['股债利差'], c, p, n) - ma_cur_to_pre(self.close, c, p, n)
    
    def alpha_ser3_12(self,c, p, n):
        return ma_cur_to_pre(self.ebs_lg['股债利差']/ser_expand_rank(self.close), c, p, n)

    # def alpha_ser3_13(self,c, p, n):
    #     sh_lagret = self.pepb_sz['close']/ self.pepb_sz['close'].shift(1)
    #     return ma_cur_to_pre(sh_lagret, c, p, n)
  
    # def alpha_ser3_14(self,c, p, n):
    #     sh_lagret = self.pepb_sh['close']/ self.pepb_sh['close'].shift(1)
    #     return ma_cur_to_pre(sh_lagret, c, p, n)

    # def alpha_ser3_15(self,c, p, n):
    #     sh_lagret = self.pepb_kc['close']/ self.pepb_kc['close'].shift(1)
    #     return ma_cur_to_pre(sh_lagret, c, p, n)
      
    # def alpha_ser3_16(self,c, p, n):
    #     sh_lagret = self.pepb_cy['close']/ self.pepb_cy['close'].shift(1)
    #     return ma_cur_to_pre(sh_lagret, c, p, n)
        
    # def alpha_ser3_17(self,c, p, n):
    #     sh_lagret = self.pepb_50['close']/ self.pepb_50['close'].shift(1)
    #     return ma_cur_to_pre(sh_lagret, c, p, n)
        
    # def alpha_ser3_18(self,c, p, n):
    #     sh_lagret = self.pepb_500['close']/ self.pepb_500['close'].shift(1)
    #     return ma_cur_to_pre(sh_lagret, c, p, n)
        
    # def alpha_ser3_19(self,c, p, n):
    #     sh_lagret = self.pepb_300['close']/ self.pepb_300['close'].shift(1)
    #     return ma_cur_to_pre(sh_lagret, c, p, n)

    # def alpha_ser3_20(self,c, p, n):
    #     sh_lagret = self.pepb_1000['close']/ self.pepb_1000['close'].shift(1)
    #     return ma_cur_to_pre(sh_lagret, c, p, n)

    def alpha_ser3_21(self,c, p, n):
        return ma_cur_to_pre(self.ampli, c, p, n)

    def alpha_ser3_22(self,c, p, n):
        return ma_cur_to_pre(self.rate_interbank['利率'] / ser_expand_rank(self.close), c, p, n)

    def alpha_ser3_23(self,c, p, n):
        return ma_cur_to_pre(self.rate_interbank['利率'], c, p, n) - ma_cur_to_pre(self.lagret, c, p, n)


# price = pd.read_pickle(r'data\stock_data\daily\price\000001.pkl.gzip')
# value = pd.read_pickle(r'data\stock_data\daily\market_value\000001.pkl.gzip')
# data = pd.concat([price,value], axis=1)
# data = data.astype(np.float32)
# ao = Alpha_ser1(data)
# t = ao.alpha_ser1_37(10)
# print('fin')
   


    

    